home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFskyedit / c / Insert < prev    next >
Encoding:
Text File  |  2003-10-16  |  12.5 KB  |  363 lines

  1. /*
  2.  *  SFskyedit - Star Fighter 3000 sky colours editor
  3.  *  Insertion dbox
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. /* RISC OS library files */
  27. #include "wimp.h"
  28. #include "toolbox.h"
  29. #include "event.h"
  30. #include "wimplib.h"
  31. #include "window.h"
  32. #include "gadgets.h"
  33.  
  34. /* My library files */
  35. #include "err.h"
  36. #include "msgtrans.h"
  37. #include "Macros.h"
  38. #include "Pal256.h"
  39.  
  40. /* Local headers */
  41. #include "Utils.h"
  42. #include "EditSky.h"
  43. #include "Back-end.h"
  44. #include "Insert.h"
  45. #include "Main.h"
  46.  
  47. /* Gadgets */
  48. #define INSERT_NUMTOINS   0x03
  49. #define INSERT_RADPLAIN   0x18
  50. #define INSERT_RADGRAD    0x19
  51.  
  52. #define INSERT_PLAINLAB   0x15
  53. #define INSERT_PLAINCOL   0x13
  54. #define INSERT_PLAINPOPUP 0x14
  55.  
  56. #define INSERT_ENDLAB     0x1c
  57. #define INSERT_ENDCOL     0x1a
  58. #define INSERT_ENDPOPUP   0x1b
  59. #define INSERT_INCEND     0x16
  60.  
  61. #define INSERT_STARTLAB   0x0c
  62. #define INSERT_STARTCOL   0x06
  63. #define INSERT_STARTPOPUP 0x1d
  64. #define INSERT_INCSTART   0x17
  65.  
  66. #define INSERT_CANCEL     0x00
  67. #define INSERT_GO         0x01
  68.  
  69. ObjectId Insert_sharedid = NULL_ObjectId;
  70. static char fill_colour, reset_colour, start_colour, end_colour;
  71. static int number;
  72. static ComponentId radio_sel;
  73. static bool have_caret;
  74.  
  75. /* ----------------------------------------------------------------------- */
  76. /*                       Function prototypes                               */
  77.  
  78. static ToolboxEventHandler _Insert_buttonhandler, _Insert_colourshandler, _Insert_popuphandler, _Insert_showhandler, _Insert_radiohandler;
  79. static void update_grad_fill(bool sel), update_plain_fill(bool sel);
  80. static _kernel_oserror *reset_start_end(ViewData *view_data);
  81.  
  82. /* ----------------------------------------------------------------------- */
  83. /*                         Public functions                                */
  84.  
  85. void Insert_initialise(ObjectId object)
  86. {
  87.   /* Record ID */
  88.   Insert_sharedid = object;
  89.  
  90.   /* Install handlers */
  91.   EF(event_register_toolbox_handler(object, Window_AboutToBeShown, _Insert_showhandler, NULL));
  92.   EF(event_register_toolbox_handler(object, ActionButton_Selected, _Insert_buttonhandler, NULL));
  93.   EF(event_register_toolbox_handler(pal256_sharedid, Pal256_ColourSelected, _Insert_colourshandler, NULL));
  94.   EF(event_register_toolbox_handler(object, PopUp_AboutToBeShown, _Insert_popuphandler, NULL));
  95.   EF(event_register_toolbox_handler(object, RadioButton_StateChanged, _Insert_radiohandler, NULL));
  96.   EF(event_register_toolbox_handler(object, Window_HasBeenHidden, hand_back_caret, &have_caret));
  97.   EF(event_register_wimp_handler(object, -1, watch_caret, &have_caret));
  98.  
  99.   /* Store initial state of dbox */
  100.   EF(numberrange_get_value(0, object, INSERT_NUMTOINS, &number));
  101.   EF(radiobutton_get_state(0, object, INSERT_RADPLAIN, NULL, &radio_sel));
  102.  
  103.   reset_colour = 0; /* black */
  104.   have_caret = false;
  105. }
  106.  
  107. /* ----------------------------------------------------------------------- */
  108. /*                         Private functions                               */
  109.  
  110. static int _Insert_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  111. {
  112.   /* Set up dialogue window */
  113.   ViewData *view_data;
  114.  
  115.   if(!E(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data)))
  116.     RE(reset_start_end(view_data))
  117.  
  118.   /* Default plain fill colour is previous value */
  119.   fill_colour = reset_colour;
  120.   char validation[16];
  121.   sprintf(validation, "r2;C/%X", (palette[fill_colour] & 0xffffff00) >> 8);
  122.   RE(button_set_validation(0, id_block->self_id, INSERT_PLAINCOL, validation))
  123.  
  124.   return 1; /* claim event */
  125. }
  126.  
  127. /* ----------------------------------------------------------------------- */
  128.  
  129. static int _Insert_colourshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  130. {
  131.   /* Colour selected by picker */
  132.   Pal256ColourSelectedEvent *pcse = (Pal256ColourSelectedEvent *)event;
  133.   ObjectId parent;
  134.   ComponentId parent_component, button;
  135.   ViewData *view_data;
  136.  
  137.   E_RETV(toolbox_get_parent(0, id_block->self_id, &parent, &parent_component), 0)
  138.   if(parent != Insert_sharedid)
  139.     return 0; /* event not handled */
  140.  
  141.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  142.  
  143.   /* Record new colour */
  144.   switch (parent_component) {
  145.     case INSERT_PLAINPOPUP:
  146.       if(fill_colour == pcse->colour_number)
  147.         return 1; /* no change */
  148.       fill_colour = pcse->colour_number;
  149.       button = INSERT_PLAINCOL;
  150.       break;
  151.  
  152.     case INSERT_STARTPOPUP:
  153.       if(start_colour == pcse->colour_number)
  154.         return 1; /* no change */
  155.       start_colour = pcse->colour_number;
  156.  
  157.       /* Should we select the 'include start colour' option? */
  158.       if(view_data->caret_position <= 0)
  159.         /* no preceding colour */
  160.         RE(optionbutton_set_state(0, id_block->parent_id, INSERT_INCSTART, 1))
  161.       else {
  162.         RE(optionbutton_set_state(0, id_block->parent_id, INSERT_INCSTART, 
  163.         start_colour != get_shade(&view_data->sky, view_data->caret_position-1)))
  164.       }
  165.       button = INSERT_STARTCOL;      
  166.       break;
  167.  
  168.     case INSERT_ENDPOPUP:
  169.       if(end_colour == pcse->colour_number)
  170.         return 1; /* no change */
  171.       end_colour = pcse->colour_number;
  172.       
  173.       /* Should we select the 'include end colour' option? */
  174.       {
  175.         int fol_colour;
  176.         if(view_data->caret_position > 62)
  177.           fol_colour = 0; /* no following colour, so use black */
  178.         else
  179.           fol_colour = get_shade(&view_data->sky, view_data->caret_position);
  180.         RE(optionbutton_set_state(0, id_block->parent_id, INSERT_INCEND, 
  181.         end_colour != fol_colour))
  182.       }
  183.       button = INSERT_ENDCOL;
  184.       break;
  185.  
  186.     default:
  187.       return 0; /* eh?! */
  188.   }
  189.  
  190.   /* Display new colour */
  191.   char validation[16];
  192.   sprintf(validation, "r2;C/%X", (palette[pcse->colour_number] & 0xffffff00) >> 8);
  193.   RE(button_set_validation(0, parent, button, validation))
  194.  
  195.   return 1; /* claim event */
  196. }
  197.  
  198. /* ----------------------------------------------------------------------- */
  199.  
  200. static int _Insert_radiohandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  201. {
  202.   RadioButtonStateChangedEvent *rbsce = (RadioButtonStateChangedEvent *)event;
  203.  
  204.   switch(id_block->self_component) {
  205.     case INSERT_RADPLAIN:
  206.       update_plain_fill(rbsce->state);
  207.       return 1; /* claim event */
  208.       
  209.     case INSERT_RADGRAD:
  210.       update_grad_fill(rbsce->state);
  211.       return 1; /* claim event */
  212.       
  213.     default:
  214.       return 0; /* unknown button */
  215.   }
  216. }
  217.  
  218. /* ----------------------------------------------------------------------- */
  219.  
  220. static int _Insert_popuphandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  221. {
  222.   /* Colour picker about to pop up - set colour */
  223.   switch(id_block->self_component) {
  224.     case INSERT_PLAINPOPUP:
  225.       RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, fill_colour))
  226.       return 1; /* claim event */
  227.  
  228.     case INSERT_STARTPOPUP:
  229.       RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, start_colour))
  230.       return 1; /* claim event */
  231.  
  232.     case INSERT_ENDPOPUP:
  233.       RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, end_colour))
  234.       return 1; /* claim event */
  235.  
  236.     default:
  237.       return 0; /* event not handled */
  238.   }
  239. }
  240.  
  241. /* ----------------------------------------------------------------------- */
  242.  
  243. static int _Insert_buttonhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  244. {
  245.   ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
  246.   ViewData *view_data;
  247.  
  248.   switch(id_block->self_component) {
  249.     case INSERT_GO:
  250.       E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  251.  
  252.       E_RETV(numberrange_get_value(0, id_block->self_id, INSERT_NUMTOINS, &number), 1)
  253.  
  254.       /* Stupidity checks */
  255.       if(view_data->selection_exists
  256.       || number < 1
  257.       || view_data->caret_position > 62) {
  258.         _kernel_oswrch(7); /* warning beep */
  259.         return 1; /* claim event */
  260.       }
  261.  
  262.       E_RETV(radiobutton_get_state(0, id_block->self_id, INSERT_RADPLAIN, NULL, &radio_sel), 1)
  263.       switch(radio_sel) {
  264.         case INSERT_RADGRAD: {
  265.           int include_start, include_end;
  266.           RE(optionbutton_get_state(0, id_block->self_id, INSERT_INCSTART, &include_start))
  267.           RE(optionbutton_get_state(0, id_block->self_id, INSERT_INCEND, &include_end))
  268.           gradient_insert(&view_data->sky, view_data->caret_position, number, start_colour, end_colour, include_start|(include_end<<1));
  269.           } break;
  270.  
  271.         case INSERT_RADPLAIN:
  272.           plain_insert(&view_data->sky, view_data->caret_position, number, fill_colour);
  273.           reset_colour = fill_colour;
  274.           break;
  275.  
  276.         default:
  277.           return 1; /* unknown operation */
  278.       }
  279.  
  280.       EditSky_rowsupdated(view_data, view_data->caret_position, 62);
  281.       EditSky_markaschanged(view_data);
  282.  
  283.       /* Move cursor after newly inserted rows */
  284.       if(view_data->caret_position + number <= 63)
  285.         EditSky_set_caretpos(view_data, view_data->caret_position + number);
  286.       else
  287.         EditSky_set_caretpos(view_data, 63);
  288.  
  289.       if(FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
  290.         RE(reset_start_end(view_data)) /* Update dbox */
  291.  
  292.       return 1; /* claim event */
  293.  
  294.     case INSERT_CANCEL:
  295.       if(FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust)) {
  296.         /* Reset dbox state */
  297.         _Insert_showhandler(Window_AboutToBeShown, NULL, id_block, handle);
  298.         RE(numberrange_set_value(0, id_block->self_id, INSERT_NUMTOINS, number))
  299.         RE(radiobutton_set_state(0, id_block->self_id, radio_sel, 1))
  300.         update_grad_fill(radio_sel == INSERT_RADGRAD);
  301.         update_plain_fill(radio_sel == INSERT_RADPLAIN);
  302.       }
  303.  
  304.       return 1; /* claim event */
  305.  
  306.     default:
  307.       return 0; /* unknown button */
  308.   }
  309. }
  310.  
  311. /* ----------------------------------------------------------------------- */
  312.  
  313. static void update_grad_fill(bool sel)
  314. {
  315.   /* Enable/disable the gradated fill controls */
  316.   RE(setgadgetfaded(Insert_sharedid, INSERT_ENDLAB, !sel))
  317.   RE(setgadgetfaded(Insert_sharedid, INSERT_ENDCOL, !sel))
  318.   RE(setgadgetfaded(Insert_sharedid, INSERT_ENDPOPUP, !sel))
  319.   RE(setgadgetfaded(Insert_sharedid, INSERT_INCEND, !sel))
  320.   RE(setgadgetfaded(Insert_sharedid, INSERT_STARTLAB, !sel))
  321.   RE(setgadgetfaded(Insert_sharedid, INSERT_STARTCOL, !sel))
  322.   RE(setgadgetfaded(Insert_sharedid, INSERT_STARTPOPUP, !sel))
  323.   RE(setgadgetfaded(Insert_sharedid, INSERT_INCSTART, !sel))
  324. }
  325.  
  326. /* ----------------------------------------------------------------------- */
  327.  
  328. static void update_plain_fill(bool sel)
  329. {
  330.   RE(setgadgetfaded(Insert_sharedid, INSERT_PLAINLAB, !sel))
  331.   RE(setgadgetfaded(Insert_sharedid, INSERT_PLAINCOL, !sel))
  332.   RE(setgadgetfaded(Insert_sharedid, INSERT_PLAINPOPUP, !sel))
  333. }
  334.    
  335. /* ----------------------------------------------------------------------- */
  336.  
  337. static _kernel_oserror *reset_start_end(ViewData *view_data)
  338. {
  339.   char validation[16];
  340.  
  341.   /* Default end colour is colour above cursor */
  342.   if(view_data->caret_position > 62)
  343.     end_colour = 0; /* no following colour, so use black */
  344.   else
  345.     end_colour = get_shade(&view_data->sky, view_data->caret_position);
  346.   sprintf(validation, "r2;C/%X", (palette[end_colour] & 0xffffff00) >> 8);
  347.   THROW(button_set_validation(0, Insert_sharedid, INSERT_ENDCOL, validation))
  348.  
  349.   /* Default is not to include the end colour */
  350.   THROW(optionbutton_set_state(0, Insert_sharedid, INSERT_INCEND, 0))
  351.  
  352.   /* Default start colour is colour below the cursor */
  353.   if(view_data->caret_position <= 0)
  354.     start_colour = 255; /* no previous colour, so use white */
  355.   else
  356.     start_colour = get_shade(&view_data->sky, view_data->caret_position-1);
  357.   sprintf(validation, "r2;C/%X", (palette[start_colour] & 0xffffff00) >> 8);
  358.   THROW(button_set_validation(0, Insert_sharedid, INSERT_STARTCOL, validation))
  359.  
  360.   /* Default is to include the start colour if we are at the bottom */
  361.   return optionbutton_set_state(0, Insert_sharedid, INSERT_INCSTART, view_data->caret_position <= 0);
  362. }
  363.